/**
 * Created by yzw on 18-1-2.
 */
layui.use(['form', 'upload'], function () {
	var form = layui.form, upload = layui.upload;

	/** markdown 编辑实时预览 **/
	convert = function () {
		var text = document.getElementById("oriContent").value;
		var converter = new showdown.Converter();
		document.getElementById("result").innerHTML = converter.makeHtml(text);
	};


	/**
	 * 保存文章
	 */
	$('#save').on('click', function () {
		var title = $('#title_pane').val();
		var content = document.getElementById("result").innerHTML;
		var classify = $('#select_classify_pane').val();
		var status = $('#status').val();
		if (title == "") {
			layer.msg('请输入标题', {offset: '100px'});
			return false;
		}
		if (title.length > 20) {
			layer.msg('标题不能大于20字', {offset: '100px'});
			return false;
		}
		if (content == "") {
			layer.msg('请输入博客内容', {offset: '100px'});
			return false;
		}
		if (classify == "") {
			layer.msg('请选择分类', {offset: '100px'});
			return false;
		}
		$.ajax({
			type: 'post',
			url: '/blog/add_blog_content',
			data: {title: title, content: content, classify: classify, status: status},
			dataType: 'json',
			success: function (data) {
				// 新增完成跳转博客列表页面
				window.location.href = '/blog/turn_blog_list';
				layer.msg(data.msg, {offset: '100px'});
			}
		});
	});

	/**
	 * 博客图片上传
	 */
	upload.render({
		elem: '#upload_img' //绑定元素
		, url: '/upload/blog_upload_img' //上传接口
		, accept: 'images'
		, exts: 'jpg|png|gif|bmp|jpeg'
		, done: function (res) {
			layer.msg(res.msg);
			$('#upload_msg').html('<span style="color: red">' + res.path + '</span>')
		}
	});
});
